package online.longzipeng.cinemasys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import online.longzipeng.cinemasys.dto.movie.MovieQueryResponseDTO;
import online.longzipeng.cinemasys.entity.Movie;
import online.longzipeng.cinemasys.entity.Type;
import online.longzipeng.cinemasys.mapper.MovieMapper;
import online.longzipeng.cinemasys.mapper.TypeMapper;
import online.longzipeng.cinemasys.service.MovieService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.longzipeng.cinemasys.vo.movie.MovieQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzp
 * @since 2021-04-09
 */
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {

    @Autowired
    MovieMapper movieMapper;
    @Autowired
    TypeMapper typeMapper;

    @Override
    public void add(Movie movie) {
        save(movie);
    }

    @Override
    public void updateMovie(Movie movie) {
        updateById(movie);
    }

    @Override
    public IPage<MovieQueryResponseDTO> getListPage(MovieQueryVO queryVO) {
        Page<MovieQueryResponseDTO> pages = new Page<>(queryVO.getPage(),queryVO.getSize());
        QueryWrapper<MovieQueryResponseDTO> qw = new QueryWrapper<>();
        // 先判断是交集还是并集 再判断查询条件 名称 导演 语言 演员 描述 持续时间 备注  分类 上映时间
        if (queryVO.getSearchType()) {
            qw.like(!StringUtils.isEmpty(queryVO.getName()),"name",queryVO.getName());
            qw.like(!StringUtils.isEmpty(queryVO.getDirector()),"director",queryVO.getDirector());
            qw.like(!StringUtils.isEmpty(queryVO.getLanguage()),"language",queryVO.getLanguage());
            qw.like(!StringUtils.isEmpty(queryVO.getActor()),"actor",queryVO.getActor());
            qw.like(!StringUtils.isEmpty(queryVO.getDescription()),"description",queryVO.getDescription());
            qw.like(!StringUtils.isEmpty(queryVO.getDuration()),"duration",queryVO.getDuration());
            qw.like(!StringUtils.isEmpty(queryVO.getNote()),"note",queryVO.getNote());
            // 分类 有二级就只查二级
            qw.eq(!StringUtils.isEmpty(queryVO.getLevelTwoTypeId()), "type_id",queryVO.getLevelTwoTypeId());
            // 分类  只有一级 就查一级里面所有二级的电影
            if (!StringUtils.isEmpty(queryVO.getLevelOneTypeId()) && StringUtils.isEmpty(queryVO.getLevelTwoTypeId())) {
                // 先查出一级分类下所有的二级分类
                QueryWrapper<Type> qwType = new QueryWrapper<>();
                qwType.eq("parent_id",queryVO.getLevelOneTypeId());
                List<Long> typeLevelTwoIds = typeMapper.selectList(qwType).stream().map(Type::getId)
                        .collect(Collectors.toList());
                qw.in("type_id",typeLevelTwoIds);
            }

            // 上映时间 同时有开始时间和结束时间就取中间值
            qw.between(!StringUtils.isEmpty(queryVO.getStartReleaseTime())&& !StringUtils
                            .isEmpty(queryVO.getEndReleaseTime()), "release_time",queryVO.getStartReleaseTime(),
                    queryVO.getEndReleaseTime());
            // 只有开始或只有结束就取一个值
            if (!StringUtils.isEmpty(queryVO.getStartReleaseTime())&& StringUtils.isEmpty(queryVO.getEndReleaseTime())
                    || StringUtils.isEmpty(queryVO.getStartReleaseTime())&& !StringUtils.
                    isEmpty(queryVO.getEndReleaseTime())) {
                qw.gt(!StringUtils.isEmpty(queryVO.getStartReleaseTime()),"release_time",
                        queryVO.getStartReleaseTime());
                qw.lt(!StringUtils.isEmpty(queryVO.getEndReleaseTime()),"release_time",
                        queryVO.getEndReleaseTime());
            }
        }else {
            qw.or().like(!StringUtils.isEmpty(queryVO.getName()),"name",queryVO.getName());
            qw.or().like(!StringUtils.isEmpty(queryVO.getDirector()),"director",queryVO.getDirector());
            qw.or().like(!StringUtils.isEmpty(queryVO.getLanguage()),"language",queryVO.getLanguage());
            qw.or().like(!StringUtils.isEmpty(queryVO.getActor()),"actor",queryVO.getActor());
            qw.or().like(!StringUtils.isEmpty(queryVO.getDescription()),"description",queryVO.getDescription());
            qw.or().like(!StringUtils.isEmpty(queryVO.getDuration()),"duration",queryVO.getDuration());
            qw.or().like(!StringUtils.isEmpty(queryVO.getNote()),"note",queryVO.getNote());
            // 分类 有二级就只查二级
            qw.or().eq(!StringUtils.isEmpty(queryVO.getLevelTwoTypeId()), "type_id",queryVO.getLevelTwoTypeId());
            // 分类  只有一级 就查一级里面所有二级的电影
            if (!StringUtils.isEmpty(queryVO.getLevelOneTypeId()) && StringUtils.isEmpty(queryVO.getLevelTwoTypeId())) {
                // 先查出一级分类下所有的二级分类
                QueryWrapper<Type> qwType = new QueryWrapper<>();
                qwType.eq("parent_id",queryVO.getLevelOneTypeId());
                List<Long> typeLevelTwoIds = typeMapper.selectList(qwType).stream().map(Type::getId).collect(Collectors.toList());
                qw.or().in("type_id",typeLevelTwoIds);
            }
            // 上映时间 同时有开始时间和结束时间就取中间值
            qw.or().between(!StringUtils.isEmpty(queryVO.getStartReleaseTime())&&!StringUtils.isEmpty(queryVO.getEndReleaseTime()),
                    "release_time",queryVO.getStartReleaseTime(),queryVO.getEndReleaseTime());
            // 只有开始或只有结束就取一个值
            if (!StringUtils.isEmpty(queryVO.getStartReleaseTime())&& StringUtils.isEmpty(queryVO.getEndReleaseTime())
                    || StringUtils.isEmpty(queryVO.getStartReleaseTime())&& !StringUtils.isEmpty(queryVO.getEndReleaseTime())) {
                qw.or().gt(!StringUtils.isEmpty(queryVO.getStartReleaseTime()),"release_time",queryVO.getStartReleaseTime());
                qw.or().lt(!StringUtils.isEmpty(queryVO.getEndReleaseTime()),"release_time",queryVO.getEndReleaseTime());
            }
        }
        // 判断排序
        if ("desc".equals(queryVO.getSort())) {
            qw.orderByDesc("id");
        }else {
            qw.orderByAsc("id");
        }
        return movieMapper.getListPage(pages,qw);
    }

    @Override
    public MovieQueryResponseDTO findById(Long id) {
        return movieMapper.findById(id);
    }

    @Override
    public List<Movie> getListJsonByKeyword(String keyWord) {
        List<Movie> movies = new ArrayList<>();
        // 判断查询条件 名称 导演 语言 演员 描述 持续时间 备注  分类 上映时间
        if (!StringUtils.isEmpty(keyWord)) {
            QueryWrapper<Movie> qwMovie = new QueryWrapper<>();
            qwMovie.like("name",keyWord);
            qwMovie.or().like("director",keyWord);
            qwMovie.or().like("director",keyWord);
            qwMovie.or().like("actor",keyWord);
            qwMovie.or().like("language",keyWord);
            qwMovie.or().like("pic",keyWord);
            qwMovie.or().like("description",keyWord);
            qwMovie.or().like("duration",keyWord);
            qwMovie.or().like("note",keyWord);
            // type_id 分类 名称查询
            QueryWrapper<Type> qwType = new QueryWrapper<>();
            qwType.like("name",keyWord).ne("parent_id",0);
            List<Long> typeIds = typeMapper.selectList(qwType).stream().map(Type::getId).collect(Collectors.toList());
            qwMovie.or().in(typeIds.size()>0,"type_id",typeIds);
            movies = movieMapper.selectList(qwMovie) ;
        }
        return movies;
    }

    @Override
    public List<MovieQueryResponseDTO> getList(MovieQueryVO queryVO) {
        return null;
    }
}
